package cj.web.marriage.controllers;

import java.util.List;

import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.http.protocol.HTTP;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.HttpClientUtil;
import cj.web.login.controllers.BaseController;
import cj.web.marriage.dao.VoteResultsCopyDAO;
import cj.web.marriage.domain.VoteResults;
import cj.web.marriage.domain.VoteResultsCopy;

@Path("/voteResultsCopy")
public class VoteResultsCopyController extends BaseController{

	private static Logger logger = Logger.getLogger(VoteResultsCopyController.class.getName());
	
	@Autowired
	private VoteResultsCopyDAO voteResultsCopyDao;
	
	@Get("show")
	public String show(Model model,VoteResults voteResults) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			List<VoteResultsCopy> objlis = voteResultsCopyDao.queryAll();
			for(VoteResultsCopy obj:objlis){
				String address="";
				
				String url = "http://opendata.baidu.com/api.php?query=" + obj.getRequestIp();
		        url += "&co=&resource_id=6006&t=1433920989928&ie=utf8&oe=gbk&format=json";
				
				String requestStr = HttpClientUtil.get(url, HTTP.UTF_8);
				JSONObject jostr = JSONObject.fromObject(requestStr);
				if(jostr.get("status").equals("0")){
					String dataStr = jostr.get("data").toString();
					JSONArray l_jostr = JSONArray.fromObject(dataStr);
					JSONObject ll_jostr = JSONObject.fromObject(l_jostr.get(0).toString());
					address=ll_jostr.get("location").toString();
				}
				
				voteResultsCopyDao.updateByVoteId(obj.getId(), address);
			}
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	public static void main(String[] args){
		//String url = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=123.123.192.255&co=&resource_id=6006&t=1433922612109&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=baidu&cb=jQuery110206955700272228569_1433922418817&_=1433922418822";
		
		String url = "http://opendata.baidu.com/api.php?query=" + "123.123.192.255";
        url += "&co=&resource_id=6006&t=1433920989928&ie=utf8&oe=gbk&format=json";
		String requestStr = HttpClientUtil.get(url, "utf-8");
		String address="";
		JSONObject jostr = JSONObject.fromObject(requestStr);
		if(jostr.get("status").equals("0")){
			String dataStr = jostr.get("data").toString();
			JSONArray l_jostr = JSONArray.fromObject(dataStr);
			JSONObject ll_jostr = JSONObject.fromObject(l_jostr.get(0).toString());
			System.out.println(ll_jostr.get("location"));
		}
		
		System.out.println(requestStr);
	}
}